package com.android.chen.mylight;

import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.Toast;


public class MainActivity extends Activity {

    private ImageButton button;
    private Camera camera = null;
    private boolean is_closed = true;
    private Camera.Parameters parameters;
    private int back = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD,
                WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.main);

        button = (ImageButton) findViewById(R.id.imageButton);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               try{
                   if(is_closed)
                   {
                       button.setBackgroundResource(R.drawable.light);
                       camera = Camera.open();
                       parameters =  camera.getParameters();
                       parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                       camera.setParameters(parameters);
                       is_closed = false;
                   }
                   else
                   {
                       button.setBackgroundResource(R.drawable.dark);
                       parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                       camera.setParameters(parameters);
                       is_closed = true;
                       camera.release();
                   }
               }
               catch (Exception e)
               {
                   e.printStackTrace();
               }
            }
        });
    }




    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            back++;
            switch (back)
            {
                case 1:
                    Toast.makeText(MainActivity.this, "Press again to exit",
                            Toast.LENGTH_SHORT).show();
                    break;
                case 2:
                    back = 0;//initialize the balue of back
                    //close app
                    if(is_closed){
                        MainActivity.this.finish();
                        //close process
                        android.os.Process.killProcess(android.os.Process.myPid());
                    }else if (!is_closed)
                    {
                        camera.release();
                        //close process
                        android.os.Process.killProcess(android.os.Process.myPid());
                        is_closed = true;
                    }
                    break;
            }
            return true;
        }else
        {
            return super.onKeyDown(keyCode, event);
        }
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
